package com.ruochen.common.utils;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ruochen.common.base.BaseConstants;
import com.ruochen.common.entity.UserInfo;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * 不使用greenDao 直接使用SharedPreferences 来保存
 */
public class SerializableSpTools {

    public static void clearLoginScope() {
        SPUtils.getInstance(BaseConstants.LOGIN_SCOPE).clear(true);
    }


    public static void putUserInfo(UserInfo UserInfo) {
        Gson gson = new Gson();
        Type type = new TypeToken<UserInfo>() {
        }.getType();
        String json = gson.toJson(UserInfo, type);
        SPUtils.getInstance(BaseConstants.LOGIN_SCOPE).put(BaseConstants.USER_INFO, json);
    }

    public static void clearUserInfo() {
        SPUtils.getInstance(BaseConstants.LOGIN_SCOPE).remove(BaseConstants.USER_INFO, true);
    }

    public static UserInfo getUserInfo() {
        try {
            String json = SPUtils.getInstance(BaseConstants.LOGIN_SCOPE)
                    .getString(BaseConstants.USER_INFO);
            if (json == null || json.equals("")) {
                return new UserInfo();
            }
            Gson gson = new Gson();
            Type type = new TypeToken<UserInfo>() {
            }.getType();
            UserInfo UserInfo = gson.fromJson(json, type);
            return UserInfo;
        } catch (Exception e) {
            return new UserInfo();
        }
    }

    public static void setIsLogin(boolean value) {
        SPUtils.getInstance(BaseConstants.LOGIN_SCOPE).put("islogin", value);
    }

    public static boolean isLogin() {
        return SPUtils.getInstance(BaseConstants.LOGIN_SCOPE).getBoolean("islogin", false);
    }

    public static void setAutoMatchLogistics(boolean value) {
        SPUtils.getInstance(BaseConstants.LOGIN_SCOPE).put("isAutoMatchLogistics", value);
    }

    public static boolean isAutoMatchLogistics() {
        return SPUtils.getInstance(BaseConstants.LOGIN_SCOPE).getBoolean("isAutoMatchLogistics", false);
    }

    public static void setFixedLogisticsName(String value) {
        SPUtils.getInstance(BaseConstants.LOGIN_SCOPE).put("fixedLogisticsName", value);
    }

    public static String getFixedLogisticsName() {
        return SPUtils.getInstance(BaseConstants.LOGIN_SCOPE).getString("fixedLogisticsName", "");
    }

}
